import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import { loginApi, getInfo, logout } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
import notification from 'ant-design-vue/es/notification'
storage.addPlugin(expirePlugin)
const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      console.log("设置roles",roles);
      state.roles = roles
      console.log("设置完成",state.roles);

    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
       console.log(userInfo);
      return new Promise((resolve, reject) => {
        loginApi(userInfo).then(response => {
          console.log("登录啊");
          console.log(response);
          if(response.code == 500){
            // notification.error({
            //   message: '错误',
            //   description: response.message
            // })
            reject(response)
          }else{
            const result = response.data
            storage.set(ACCESS_TOKEN, result.token, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result.token)
            resolve()
          }
         
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
       console.log("获取用户信息");
        // 请求后端获取用户信息 /api/user/info
        getInfo().then(response => {
     
          console.log(12);
          
          if (response.data.roles && response.data.permissions.length > 0) {
            console.log("完成1");
            const role = response.data.roles
            // role.permissions = result.role.permissions.map(permission => {
            //   const per = {
            //     ...permission,
            //     actionList: (permission.actionEntitySet || {}).map(item => item.action)
            //    }
            //   return per
            // })
           // role.permissionList = role.permissions.map(permission => { return permission.permissionId })
            // 覆盖响应体的 role, 供下游使用
            //result.data.roles = role
            console.log(role);
            commit('SET_ROLES', role)
            commit('SET_INFO', response.data.user)
            commit('SET_NAME', { name: response.data.user.nickName, welcome: welcome() })
            commit('SET_AVATAR', response.data.user.avatar)
             
            // 下游
            resolve(response)
            console.log("完成2");
          } else {
            console.log("erro");
            reject(new Error('getInfo: roles must be a non-null array !'))
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          storage.remove(ACCESS_TOKEN)
          resolve()
        }).catch((err) => {
          console.log('logout fail:', err)
          // resolve()
        }).finally(() => {
        })
      })
    }

  }
}

export default user
